<template>
  <view class="page">
    <nav-bar @load="handleLoadNav">
      <view class="uni-easyinput-head">
        <uni-easyinput prefixIcon="search" disableColor="#fff" placeholder="请输入内容"  @confirm="handleQueryChange" @input="handleQueryChange"
          @clear="handleClear"></uni-easyinput>
      </view>
    </nav-bar>
    <view class="content" :style="{ height: contentHeight + 'px' }">
      <custom-scroll-view :loadData="handleLoadData" ref="listRef" emptyText="暂无文章">
        <template v-slot="{data}">
          <view class="article-list-content">
            <view v-for="(item,idx) in data.data" :key="item.intactArticleId" class="article-list-item">
              <article-block :data="item" :userShow="true" @giveDeleteArticle="takeDeleteArticle"
                @giveSetPrivacy="takeSetPrivacy" @share="handleArticleShare">
              </article-block>
            </view>
          </view>
        </template>
      </custom-scroll-view>
    </view>
  </view>
</template>

<script>
  import {
    getUserArticleApi,
    putSetPrivacyApi,
    deleteIssueApi
  } from "@/api/my/issue.js"
  export default {
    mixins: [],
    data() {
      return {
        contentHeight: 0,
        shareData: {}
      };
    },
    onLoad() {
      this.$nextTick(() => {
        this.handleClear();
      })
    },
    onShareAppMessage() {
      return this.shareData;
    },
    methods: {
      handleLoadNav({
        navHeight,
        height
      }) {
        this.contentHeight = height - navHeight;
      },
      handleArticleShare(data) {
        this.shareData = data;
      },
      handleClear() {
        this.query = "";
        this.$refs.listRef.handleRefresher(true);
      },
      // 加载数据
      async handleLoadData(page) {
        let {
          data: res
        } = await getUserArticleApi(page, this.query);
        let data = {
          data: res.success ? res.data.list.records : [],
          pages: res.success ? res.data.list.pages : 0,
          success: res.success
        };
        return data;
      },
      // 搜索内容发生变化
      handleQueryChange(query) {
        this.query = query;
        this.$refs.listRef.handleRefresherLoad();
      },
      // 删除发布社区
      takeDeleteArticle(id) {
        wx.showModal({
          title: '是否删除该文章？',
          success: async (res) => {
            if (res.confirm) {
              const {
                data: result
              } = await deleteIssueApi(id)
              if (result.success) {
                uni.showToast({
                  title: "删除成功",
                  icon: "none"
                })
                const index = this.$refs.listRef.data.data.findIndex(item => item
                  .articleId == id)
                if (index != -1) {
                  this.$refs.listRef.data.data.splice(index, 1)
                }
              } else {
                uni.showToast({
                  title: "删除失败",
                  icon: "none"
                })
              }
            }
          }
        })
      },
      // 设为私密,公开
      async takeSetPrivacy(id, privacyState) {
        let privacyStateMet = privacyState == 1 ? 0 : 1
        const {
          data: result
        } = await putSetPrivacyApi(id, privacyStateMet)
        if (result.success) {
          this.$refs.listRef.data.data.find(item => item.articleId == id).privateState = privacyStateMet
        } else {
          uni.showToast({
            title: "修改失败",
            icon: "none"
          })
        }
      },
    }
  }
</script>

<style lang="scss" scoped>
  .page {
    width: 100%;

    .content {
      width: 100%;
    }

    .article-list-content {
      width: 100%;
      padding: 32rpx;
      box-sizing: border-box;

      .article-list-item {
        margin-bottom: 24rpx;
      }
    }
  }
</style>